/**
 * Copyright(c) 2011
 *
 * Licensed under the terms of the Open Source LGPL 3.0
 * http://www.gnu.org/licenses/lgpl.html
 * @author Greivin Britton, brittongr@gmail.com
 *     
 * @changes
 * No currency symbol by default    
 * No decimalPrecision in config
 * Supporting any character as thousand separator
 * Improved getFormattedValue
 * Removed unnecessary code to create format template, now using float.toFixed(this.decimalPrecission)    
 */
Ext.define('Ext.ux.NumericField', {
    extend: 'Ext.form.TextField',
    alias: 'widget.numericfield',

	style: 'text-align: left;',
	minText : "El valor m&#237;nimo para este campo es de {0}",
    maxText : "El valor m&#225;ximo para este campo es de {0}",
    nanText : "{0} no es un n&#250;mero v&#225;lido",
    zeroText: "El valor m&#237;nimo para este campo es de 0.00",
    negativeText: "El valor para este campo no puede ser negativo",
    thousandSeparator: ',',
    decimalSeparator: '.',
    useThousandSeparator: true,
    allowDecimals: true,
    allowBlank: false,
    baseChars : '0123456789',
	fieldCls: "number-field",
	allowZero: true,
	
	initComponent: function() {
        var me = this;
        me.callParent();
        
        if (me.disableKeyFilter !== true) {
            allowed = me.baseChars + '';
            if (me.allowDecimals) {
                allowed += me.decimalSeparator;
            }
            if (me.minValue < 0) {
                allowed += '-';
            }
            if(me.useThousandSeparator === true && !Ext.isEmpty(me.thousandSeparator)){
            	allowed += me.thousandSeparator;
            }
            allowed = Ext.String.escapeRegex(allowed);
            me.maskRe = new RegExp('[' + allowed + ']');
            if (me.autoStripChars) {
                me.stripCharsRe = new RegExp('[^' + allowed + ']', 'gi');
            }
        }
        
    },
    
    getErrors: function(value){
    	var me = this,
	    	errors = me.callParent(arguments),
	    	format = Ext.String.format;
    	value = Ext.isEmpty(value) ? 0 : value.toString().replace(/\,/gi, "");
    	value = isNaN(value) ? 0 : parseFloat(value);
    	if(me.allowZero === false && value == 0)
    		errors.push(format(me.zeroText, value));
    	if(value < 0)
    		errors.push(format(me.negativeText, value));
    	return errors;
    },
    
	setValue: function(v){
	   if(!Ext.isEmpty(v)) v = v.toString().replace(/\,/gi, '');
	   Ext.ux.NumericField.superclass.setValue.call(this, v);
	   this.setRawValue(this.getFormattedValue(this.getValue()));
    },
    
	getValue: function(){
		var me = this;
		return Ext.isEmpty(me.getRawValue()) ? "" : me.getRawValue().toString().replace(/\,/gi, "");
	},
	
    getSubmitValue: function() {
    	var value = this.getRawValue();
    	value = Ext.isEmpty(value) ? null : value.toString().replace(/\,/gi, "");
        return this.processRawValue(value);
    },
	
    getFormattedValue: function(v){
    	var me = this;
		if (Ext.isEmpty(v)) 
            return '0' + me.decimalSeparator + '00';
	    else 
	    	return Ext.util.Format.webMoney(v);
    },
    
    beforeBlur : function() {
        var me = this,
        	value = me.getRawValue();
        	v = '';
        	
        if(!Ext.isEmpty(value))
            v = value.replace(/\,/gi, '');

        if (!Ext.isEmpty(v)) {
            me.setValue(v);
        }
    }
});